﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using VSSAHT;
using VSS_AHT.Properties;
namespace VSS_AHT
{
    public partial class FormPrinter : DevExpress.XtraEditors.XtraForm
    {

        #region Declare Availables
        private readonly SettingApp _settings = new SettingApp();
        #endregion

        #region Singleton Members
        private static volatile FormPrinter instance;
        private static object syncRoot = new Object();

        public static FormPrinter Instance
        {
            get
            {
                lock (syncRoot)
                {
                    if (instance == null)
                        instance = new FormPrinter();
                }

                return instance;
            }
        }
        #endregion

        #region Ctors
        public FormPrinter()
        {
            InitializeComponent();
            Initialize();
        }
        public void Initialize()
        {
            txtPrinterReceipt.Text = _settings.ReceiptPrintName;
            txtPrinterReport.Text = _settings.ReportPrintName;
            chkPreviewReceipt.Checked = _settings.IsPreviewReceiptPrint;
            chkPreviewReport.Checked = _settings.IsPreviewReportPrint;

            txtMarginLeft.Text = _settings.MarginLeft.ToString();
            txtMarginRight.Text = _settings.MarginRight.ToString();
            txtMarginTop.Text = _settings.MarginTop.ToString();
            txtMarginBottom.Text = _settings.MarginBottom.ToString();

            if (_settings.isLinePrint)
            {
                rdbPrintLine.Checked = true;
            }
            else
            {
                rdbPrintOrder.Checked = true;
            }

            this.btnSave.Click += new EventHandler(delegate { ClickButtonSave(); });
            this.btnCancel.Click += new EventHandler(delegate
            {
                this.Close();
            });

            this.Disposed += new EventHandler(delegate { instance = null; });
            //printer
            #region Printer
            this.btnPrinterReceipt.Click += new EventHandler(delegate
            {
                PrintDialog printDialog1 = new PrintDialog();
                printDialog1.ShowDialog();
                txtPrinterReceipt.Text = printDialog1.PrinterSettings.PrinterName.ToString();
            });

            this.btnPrinterReport.Click += new EventHandler(delegate
            {
                PrintDialog printDialog1 = new PrintDialog();
                printDialog1.ShowDialog();
                txtPrinterReport.Text = printDialog1.PrinterSettings.PrinterName.ToString();
            });
            #endregion
        }
        #endregion

        #region Methods
        private void ClickButtonSave()
        {
            try
            {

                _settings.ReceiptPrintName = txtPrinterReceipt.Text;
                _settings.ReportPrintName = txtPrinterReport.Text;
                _settings.IsPreviewReceiptPrint = chkPreviewReceipt.Checked;
                _settings.IsPreviewReportPrint = chkPreviewReport.Checked;

                _settings.MarginLeft = Convert.ToInt32(txtMarginLeft.Text.Trim());
                _settings.MarginRight = Convert.ToInt32(txtMarginRight.Text.Trim());
                _settings.MarginTop = Convert.ToInt32(txtMarginTop.Text);
                _settings.MarginBottom = Convert.ToInt32(txtMarginBottom.Text);

                if (rdbPrintLine.Checked)
                {
                    _settings.isLinePrint = true;
                }
                else
                {
                    _settings.isLinePrint = false;
                }

                Settings.Default.Save();

               // Common.InitPort();

                Common.ShowSuccessful("Lưu cấu hình thành công!");
            }
            catch (Exception ex)
            {
                Common.ShowAlert("Lưu cấu hình thất bại => ERROR! " + ex.Message);
            }
        }
        #endregion


        #region Events
        protected override void OnClosed(EventArgs e)
        {
            instance = null;
            Settings.Default.Reload();
            base.OnClosed(e);
        }
        #endregion
    }
}